package com.yyoo.doc.mypdf.builder;

import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.yyoo.doc.html.builder.StyleBuilder;
import com.yyoo.doc.html.style.StyleAttr;

import java.awt.Color;

public class PdfStyleBuilder extends StyleBuilder {
    /**
     * 设置 border-width 属性的值
     * @param rectangle 矩形对象
     */
    protected void setBorderWidth(Rectangle rectangle){

        Float borderWidth = converToFloat(StyleAttr.BORDER_WIDTH);
        if(borderWidth != null){
            rectangle.setBorderWidth(borderWidth);
        }

        Float borderWidthTop = converToFloat(StyleAttr.BORDER_TOP_WIDTH);
        if(borderWidthTop != null){
            rectangle.setBorderWidthTop(borderWidthTop);
        }

        Float borderWidthRight = converToFloat(StyleAttr.BORDER_RIGHT_WIDTH);
        if(borderWidthRight != null){
            rectangle.setBorderWidthRight(borderWidthRight);
        }

        Float borderWidthBottom = converToFloat(StyleAttr.BORDER_BOTTOM_WIDTH);
        if(borderWidthBottom != null){
            rectangle.setBorderWidthBottom(borderWidthBottom);
        }

        Float borderWidthLeft = converToFloat(StyleAttr.BORDER_LEFT_WIDTH);
        if(borderWidthLeft != null){
            rectangle.setBorderWidthLeft(borderWidthLeft);
        }
    }

    /**
     * 设置 border-color 属性的值
     * @param rectangle 矩形对象
     */
    protected void setBorderColor(Rectangle rectangle){

        Color borderColor = converToColor(StyleAttr.BORDER_COLOR);
        if(borderColor != null){
            rectangle.setBorderColor(borderColor);
        }

        Color borderColorTop = converToColor(StyleAttr.BORDER_TOP_COLOR);
        if(borderColorTop != null){
            rectangle.setBorderColorTop(borderColorTop);
        }

        Color borderColorRight = converToColor(StyleAttr.BORDER_RIGHT_COLOR);
        if(borderColorRight != null){
            rectangle.setBorderColorRight(borderColorRight);
        }
        Color borderColorBottom = converToColor(StyleAttr.BORDER_BOTTOM_COLOR);
        if(borderColorBottom != null){
            rectangle.setBorderColorBottom(borderColorBottom);
        }
        Color borderColorLeft = converToColor(StyleAttr.BORDER_LEFT_COLOR);
        if(borderColorLeft != null){
            rectangle.setBorderColorLeft(borderColorLeft);
        }
    }

    /**
     * 设置 padding 属性值
     * @param cell 表格单元格对象
     */
    protected void setPadding(PdfPCell cell){
        Float padding = converToFloat(StyleAttr.PADDING);
        if(padding != null){
            cell.setPadding(padding);
        }

        Float paddingTop = converToFloat(StyleAttr.PADDING_TOP);
        if(paddingTop != null){
            cell.setPaddingTop(paddingTop);
        }

        Float paddingRight = converToFloat(StyleAttr.PADDING_RIGHT);
        if(paddingRight != null){
            cell.setPaddingRight(paddingRight);
        }

        Float paddingBottom = converToFloat(StyleAttr.PADDING_BOTTOM);
        if(paddingBottom != null){
            cell.setPaddingBottom(paddingBottom);
        }

        Float paddingLeft = converToFloat(StyleAttr.PADDING_LEFT);
        if(paddingLeft != null){
            cell.setPaddingLeft(paddingLeft);
        }

    }

    /**
     * 设置背景颜色
     */
    protected void setBackgroundColor(Rectangle rectangle) {
        Color backgroundColor = converToColor(StyleAttr.BACKGROUND_COLOR);
        if(backgroundColor != null){
            rectangle.setBackgroundColor(backgroundColor);
        }
    }

    /**
     * 设置位置
     */
    protected void setPostion(Rectangle rectangle){
        Float left = converToFloat(StyleAttr.LEFT);
        if(left != null){
            rectangle.setLeft(left);
        }

        Float bottom = converToFloat(StyleAttr.BOTTOM);
        if(bottom != null){
            rectangle.setBottom(bottom);
        }

        Float top = converToFloat(StyleAttr.TOP);
        if(top != null){
            rectangle.setTop(top);
        }

        Float right = converToFloat(StyleAttr.RIGHT);
        if(right != null){
            rectangle.setRight(right);
        }
    }

    /**
     * 获取标签的 align 属性值
     */
    protected Integer getTagAlign(){
        String styleValue = getHtmlEl().getAttributeValue("align");
        if(styleValue != null){
            if(styleValue.equalsIgnoreCase("left")){
                return Element.ALIGN_LEFT;
            }else if(styleValue.equalsIgnoreCase("right")){
                return Element.ALIGN_RIGHT;
            }else if(styleValue.equalsIgnoreCase("center")){
                return Element.ALIGN_CENTER;
            }
        }
        // 如果 align 属性为空，则取 style 中的 align 属性值
        return converToInteger(StyleAttr.ALIGN);
    }

    /**
     * 获取 font 字体对象
     */
    protected Font getFont(){
        String fontFamily = getStyleMap().get(StyleAttr.FONT_FAMILY);
        if(fontFamily == null){
            fontFamily = "宋体";
        }

        float fontSize = converToFloat(StyleAttr.FONT_SIZE);
        int style = converToInteger(StyleAttr.FONT_STYLE);
        Color color = converToColor(StyleAttr.COLOR);

        return FontFactory.getFont(fontFamily, BaseFont.IDENTITY_H,fontSize,style,color);
    }
}
